﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


using CRM.Entity;
using CRM.BLL;
using DP.Web.UI;
using DP.Common;

namespace CRM.Web.MessagingManage
{
    public partial class EmailSend : Common.PageBase
    {

        T_EMAIL_SEND_BLL bll = new T_EMAIL_SEND_BLL();
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            PageHelper.SetPrePageUrl(this.Page);
            PageHelper.SetGoBackUrl(this.Page, btnCancel, StaticConstant.NotBackUrl);

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                


                pageDataInit();
            }
        }

        #region 页面初始化数据绑定

 












   

        #endregion

        private void pageDataInit()
        {
            //string _autoid = String.IsNullOrEmpty(Request.QueryString["AUTOID"]) ? "" : Request.QueryString["AUTOID"].Trim();

            //if (!String.IsNullOrEmpty(_autoid))
            //{
            //    T_EMAIL_SEND_Entity entity = bll.GetEntity("AUTOID", _autoid);

            //    PageHelper.SetControlValue<T_EMAIL_SEND_Entity>(this.Page, entity);
            //}


        }

        /// <summary>
        /// Handles the Click event of the btnUpload control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void btnUpload_Click(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            if(btn == null)
            {
                return;
            }
            string number = btn.ID.ToLower().Replace(btn.CommandName.ToLower(), "");
            FileUpload ImportFile = Page.FindControl("ImportFile" + number) as FileUpload;
            if(ImportFile == null)
            {
                return;
            }
            TextBox txtATTACH = Page.FindControl("txtATTACH" + number) as TextBox;
            if(txtATTACH == null)
            {
                return;
            }

            Label lblATTACH = Page.FindControl("lblATTACH" + number) as Label;
            if (lblATTACH == null)
            {
                return;
            }

           
            string filepath = UploadFile(this.Page, ImportFile);
            string filename = GetFileName(filepath);
            if (string.IsNullOrEmpty(filepath))
            {
                PageHelper.ShowMessage(this.Page, "文件上传失败！");
                return;
            }

            btnOK.Enabled = true;
            txtATTACH.Text = StaticConstant.UploadFilePath + "\\" + filename;
            lblATTACH.Text = GetFileName(ImportFile.PostedFile.FileName);
        }


        /// <summary>
        /// 文件 上传方法
        /// </summary>
        /// <param name="page">当前页面</param>
        /// <param name="auc">上传控件</param>
        /// <returns>上传后的文件的服务器路径</returns>
        private string UploadFile(Page page, System.Web.UI.WebControls.FileUpload auc)
        {
            string temp = DateTime.Now.ToString("yyyyMMddHHmmss");
            string filename = auc.PostedFile.FileName;
            filename = GetFileName(filename);

            string filepath = HttpContext.Current.Request.MapPath(StaticConstant.UploadFilePath);

            int index = filename.LastIndexOf('.');
            if (index != -1)
            {
                filename = filename.Insert(index, temp);
            }
            else
            {
                filename += temp;
            }

            string fullpath = string.Empty;
            if (filepath.LastIndexOf("\\") == filepath.Length - 1)
            {
                fullpath = filepath + filename;
            }
            else
            {
                fullpath = filepath + "\\" + filename;
            }

            try
            {
                auc.PostedFile.SaveAs(fullpath);
            }
            catch
            {
                fullpath = null;
            }
            return fullpath;
        }

        /// <summary>
        /// 在绝对路径中，获取Excel文件名
        /// </summary>
        /// <param name="input">需要导入的Excel绝对路径</param>
        /// <returns></returns>
        private string GetFileName(string input)
        {
            return input.Substring(input.LastIndexOf("\\") + 1);
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            T_EMAIL_SEND_Entity entity = new T_EMAIL_SEND_Entity();

            PageHelper.GetControlValue<T_EMAIL_SEND_Entity>(this.Page, ref entity);
            entity.BUSINESSDIVISIONCODE = _userLoginInfo.UserEntity.BUSINESSDIVISIONCODE;
            entity.BUSINESSDIVISIONID = _userLoginInfo.UserEntity.BUSINESSDIVISIONID;
            entity.BUSINESSDIVISIONNAME = _userLoginInfo.UserEntity.BUSINESSDIVISIONNAME;
            entity.CREATETIME = DateTime.Now;
            entity.SDATE = DateTime.Now.ToString("yyyyMMdd");
            entity.STIME = DateTime.Now.ToString("HHmmss");
            entity.GHID = _userLoginInfo.UserEntity.USERID.ToString();
            entity.FROM_NAME = _userLoginInfo.UserEntity.USERNAME;
            entity.FROM_ADDR = _userLoginInfo.UserEntity.EMAIL;

            long rel = bll.Insert(entity);
            if (rel > 0)
            {
                PageHelper.ShowMessage(this.Page, "提交发送成功！", "EmailSendList.aspx");
            }
            else
            {
                PageHelper.ShowMessage(this.Page, "提交发送失改！");
            }

        }
    }
}
